<template>
	<view class="pages">
		<view>
			<button @click="getRsaKeys">生成公私钥</button>
			<button @click="getOpreate">加解密消息</button>
			<view>
				公钥publicKey: <view>{{ publicKey }}</view>
				<!-- 私钥privateKey: <view>{{ privateKey }}</view> -->
			</view>
			<view>
				密文: <textarea v-model="encryptedText"></textarea>
				明文: <textarea v-model="decryptedText"></textarea>
			</view>
		</view>
	</view>
</template>

<script>
	import MyJsrsasign from '@/common/jsrsasign.js'
	import {
		KJUR,
		KEYUTIL,
		RSAKey
	} from 'jsrsasign';
	export default {
		components: {},
		data() {
			return {
				publicKey: null,
				privateKey: null,
				encryptedText: '',
				decryptedText: '',
				key: {},
			}
		},
		onShow() {
			console.log('加密数据:', "Hello, World!");
			const binaryString ="Hello, World!"
			const base64String = btoa(binaryString);
			console.log('加密后的数据:', base64String);
			const decodedString = atob(base64String);
			console.log('解密后的数据:', decodedString);
			
			// console.log("MyJsrsasign:",MyJsrsasign.getWays());
			console.log("this.$jsrsasign：", this.$jsrsasign);
			// console.log("KJUR：", KJUR);
			// console.log("KEYUTIL：", KEYUTIL);
			// this.key = MyJsrsasign.getWays();
			// console.log("key：",this.key)
		},
		methods: {
			getRsaKeys() {
				let that = this;
				// var rsaKeypair = that.$jsrsasign.KEYUTIL.generateKeypair("RSA", 2048);
				// that.privateKey = that.$jsrsasign.KEYUTIL.getPEM(rsaKeypair.prvKeyObj, "PKCS8PRV");
				// that.publicKey = that.$jsrsasign.KEYUTIL.getPEM(rsaKeypair.pubKeyObj);
				// console.log('公钥:', that.publicKey);
				// console.log('私钥:', that.privateKey);
				// return rsaKeypair;
				that.privateKey = `-----BEGIN PRIVATE KEY-----
				MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCIbjxqNBIBWubw
				iOB6tkgyxBX7ZFF+KE5lM54eh0IjYSymOii1PShP1g1yY+lpu+8OCtk9S2DTFEEM
				MO1ImD4n3ewF7TzVzb/HPdr3E31CGHli2Igh9XNmny553H1mlsOR6OyFB2Ewq4cg
				pVH167enKKim62jxIf37uJeCUMGRqRH20eGYHB+DYcxxscqxTFQopEdexELtZ7XB
				G+bvNxajCUv6APskpYyHYXOSUJRKrDrJFSzQ+Yngj4ka2ELs1xf8ONJfYI7nig2s
				x2xAIPBo3Y5tjKJgLbjgqpxWT0RdVQ0Dm0yZBfkWFwTZJuPM0mw+hEzjyP59ulVb
				+N64gUfRAgMBAAECggEAPHY+KALzh841UpwWf8uQq47i/woyY2qRc6Awju76jJtg
				IjrcF5YG63q9cth8dPNouhxsshFZ07HCMhjsz6NIyHo9Q1MkBsysFSyAgccNV+Fo
				yjmT+xjAD9azziW9HV3rRhqn1zyyAdPCiuE5NXdFaLtIItFpLJQX7xqbapFHd7mA
				RJcbmQPWqioUBUzkLsLszGw5dTNYLeQ5AIMWKyHvpFzdO/b7c8n5UJFoFsXyteYr
				aR0Ruci1VXn04duhimwpPTzY8gc3T/Ljmkp14iPUw+/t9thKpnNdMjsWiNXNa43I
				yM8P2sWEDhjMAlZfRXNBg8lcgU0bBZPbbyeRMs4KUQKBgQDoFF0MTuiCZnRyx8Xd
				Q6Fl2bOcfrmecb4HcygaGtxyNswrmVRjOJ+j8g6FA/BcX3xUDO8MzbmENsM1LTR5
				eZONaBhHglE5YendIRoTtk68EbMQZu8dZNYnF03vl1VEdvu99NYbfSyrQpuomXg3
				J69gaWR7MYJsrcZ/2E0FpevQ7wKBgQCWfhXriCzh6q35dAqEb8F2whR4zEFiXZXO
				RqdE5RCdGrhw3JmhYbKoSaOF8o7QnhKXYMtQShhLoP19iHK29SKUQw2gIiBr/Opf
				vn/T5ZRfo1CNjLZlymNHIj31lluSGRcp+tn8zoS7sJr3mRujnvHWNxzs7Sz4Yten
				p2UwqjrzPwKBgQCSAAWNcbLKHkRR1SXADgTiOzPIVtzVNb8Tlrl6ur1GSPzmzL4O
				8ZdUnznNKm4y3osroTtd9aoo5/6kXHzvkrFluz/S9MO+cr+aMnHiRsV/gS8COY9i
				EYa77uhDuxOUT7oNhZKQlo3ca7nbNjkvkZK992+UJNBlKGmBEgVacUhKFwKBgG4U
				eHA6wanXAb8QVIkfPgJiYJsjvUf4cJXcNIo+i/J/Ex7rHi1fy1Ex3D0keba7iZnV
				IaLvL+pSLlnerxqJIOwWSCc0D0cuTzoNnG8g5XE3grqnjNvUON5Ve8DiZMTvOXRp
				m35CifDIc/Jcukzq/LbWxX/A1T4y/tARsADy4//9AoGAan8Ky6/0bBqWP/8ZQmeX
				LIKt1Zx1ku/dcZk4odE6LGudkJ2urcpx7j6/nQRAOAR0kx3tDlQ2lVTKfg3lF2v1
				PaIC+ZS2ojniPpiSi5OhDy/ra/VEZRdWodhTaBBwpxay1NO/poJ6bf7N6VW7UgEG
				uJqJqpuiYqxRbal4onP7z0Y=
				-----END PRIVATE KEY-----`;
				that.publicKey = `-----BEGIN PUBLIC KEY-----
				MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiG48ajQSAVrm8IjgerZI
				MsQV+2RRfihOZTOeHodCI2EspjootT0oT9YNcmPpabvvDgrZPUtg0xRBDDDtSJg+
				J93sBe081c2/xz3a9xN9Qhh5YtiIIfVzZp8uedx9ZpbDkejshQdhMKuHIKVR9eu3
				pyioputo8SH9+7iXglDBkakR9tHhmBwfg2HMcbHKsUxUKKRHXsRC7We1wRvm7zcW
				owlL+gD7JKWMh2FzklCUSqw6yRUs0PmJ4I+JGthC7NcX/DjSX2CO54oNrMdsQCDw
				aN2ObYyiYC244KqcVk9EXVUNA5tMmQX5FhcE2SbjzNJsPoRM48j+fbpVW/jeuIFH
				0QIDAQAB
				-----END PUBLIC KEY-----`;
			},
			getOpreate() {
				let that = this;
				const data = 'Hello World!';
				const encryptedData = that.encryptData(data);
				console.log('加密后的数据:', encryptedData);
				that.encryptedText = encryptedData;
				const decryptedData = that.decryptData(encryptedData);
				console.log('解密后的数据:', decryptedData);
				that.decryptedText = decryptedText;
			},
			// 加密数据
			encryptData(data) {
				let that = this;
				console.log("that.publicKey：", that.publicKey);
				// var pub = that.privateKey;
				var pub = that.$jsrsasign.KEYUTIL.getKey(that.publicKey);
				console.log("pub：", pub);
				const encrypted = that.$jsrsasign.KJUR.crypto.Cipher.encrypt(data,pub,"RSAOAEP");
				console.log("encrypted:", encrypted);
				const singResult = that.$jsrsasign.hextob64(encrypted);
				console.log("sign加密结果是"+singResult);
				// return encrypted;
			},
			// 解密数据
			decryptData(encryptedData) {
				let that = this;
				console.log("that.privateKey：", that.privateKey);
				const decrypted = that.$jsrsasign.KJUR.crypto.Cipher.decrypt(encryptedData, that.privateKey);
				return decrypted;
			},
		}
	}
</script>

<style>
</style>